﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    public partial class MainService : IComment 
    {
        public DataSet Comment_GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Comment_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet Comment_GetList( out int ItemCount,int currentPage, int pageSize)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Comment_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet Comment_GetWithEmployee(int EmployeeID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Comment_GetWithEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Comment_EmployeeID", EmployeeID);

            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);

            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public CommentInfo Comment_GetInfo(int CommentID)
        {
            CommentInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Comment_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Comment_ID", CommentID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new CommentInfo();
                    retVal.ID = Convert.ToInt32(dr["Comment_ID"]);
                    retVal.EmployeeID = Convert.ToInt32(dr["Comment_EmployeeID"]);
                    retVal.Date = Convert.ToDateTime(dr["Comment_Date"]);
                    retVal.Kind = dr["Comment_Kind"].ToString();
                    retVal.Speciality = dr["Comment_Speciality"].ToString();
                    retVal.Morality = dr["Comment_Morality"].ToString();
                    retVal.Discipline = dr["Comment_Discipline"].ToString();
                    //retVal.Request = dr["Comment_Request"].ToString();                                       
                    retVal.Description = dr["Comment_Description"].ToString();
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int Comment_Insert(CommentInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Comment_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Comment_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@Comment_Date", eInfo.Date);
            dbCmd.Parameters.AddWithValue("@Comment_Kind", eInfo.Kind);
            dbCmd.Parameters.AddWithValue("@Comment_Speciality", eInfo.Speciality);
            dbCmd.Parameters.AddWithValue("@Comment_Morality", eInfo.Morality);
            dbCmd.Parameters.AddWithValue("@Comment_Discipline", eInfo.Discipline);
            dbCmd.Parameters.AddWithValue("@Comment_Description", eInfo.Description);
            //dbCmd.Parameters.AddWithValue("@Comment_Request", eInfo.Request);
            dbCmd.Parameters.AddWithValue("@Comment_Accept", eInfo.Accept);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool Comment_Update(CommentInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Comment_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("Comment_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@Comment_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@Comment_Date", eInfo.Date);
            dbCmd.Parameters.AddWithValue("@Comment_Kind", eInfo.Kind);
            dbCmd.Parameters.AddWithValue("@Comment_Speciality", eInfo.Speciality);
            dbCmd.Parameters.AddWithValue("@Comment_Morality", eInfo.Morality);
            dbCmd.Parameters.AddWithValue("@Comment_Discipline", eInfo.Discipline);
            dbCmd.Parameters.AddWithValue("@Comment_Description", eInfo.Description);
            //dbCmd.Parameters.AddWithValue("@Comment_Request", eInfo.Request);
            dbCmd.Parameters.AddWithValue("@Comment_Accept", eInfo.Accept);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool Comment_UpdateEmployee(int EmployeeID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Comment_UpdateEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool Comment_Delete(int CommentID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Comment_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Comment_ID", CommentID);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}

